package com.emm.yixun.website.controller.tools;

import com.alibaba.fastjson.JSONArray;
import com.emm.yixun.common.model.*;
import com.emm.yixun.common.model.dto.*;
import com.emm.yixun.common.model.enums.*;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.customer.service.CustomerServiceFacade;
import com.emm.yixun.estate.service.ProjectPremisesServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.estate.service.SaleReturnPmoServiceFacade;
import com.emm.yixun.system.service.BaseCodeValueServiceFacade;
import com.emm.yixun.website.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * 工具类
 * 初始化项目相关内容工具
 *
 */
@Controller
@RequestMapping("/init/project/")
public class InitProjectSalePlanController extends BaseController{

	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	@Autowired
	private ProjectPremisesServiceFacade projectPremisesServiceFacade;
	@Autowired
	private SaleReturnPmoServiceFacade saleReturnPmoServiceFacade;
	@Autowired
	private BaseCodeValueServiceFacade baseCodeValueServiceFacade;
	@Autowired
	private CustomerServiceFacade customerServiceFacade;
	//初始化绿城销售回款数据
	@RequestMapping(value = "initSaleReturnPmo")
	public void initSaleReturnPmo() {

		BaseCodeValueDto bcvDto = new BaseCodeValueDto();
		bcvDto.setMerchantId(Long.valueOf(MerchantEnum.GREEN_TOWN.getNo()));
		bcvDto.setTypeName(BaseCodeTypeEnum.getByNo(BaseCodeTypeEnum.TYPE_TWELVE.getNo()).getDes());
		bcvDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		// *1解决数字排序问题.例如 1,2,10.排序后会显示成 1,10,2
		bcvDto.setOrders("t.sort*1");
		JSONArray dtArray = JSONArray.parseArray(baseCodeValueServiceFacade.getBcvJsonArray(bcvDto));
		System.out.println(baseCodeValueServiceFacade.getBcvJsonArray(bcvDto));
		System.out.println(dtArray.size());
		String[] monthArr = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",};
		ProjectDto projectDto = new ProjectDto();
		projectDto.setMerchantId(Long.valueOf(MerchantEnum.GREEN_TOWN.getNo()));
		projectDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		ResponseList<Project> projectResList = projectServiceFacade.findByDto(projectDto);
		List<Project> projectList = projectResList.getEntity();

		Calendar calendar = Calendar.getInstance();
		String year = String.valueOf(calendar.get(Calendar.YEAR));

		SaleReturnPmoDto srpDto = new SaleReturnPmoDto();
		srpDto.setSaleYear(year);
		ResponseList<SaleReturnPmo> srpResList = saleReturnPmoServiceFacade.findByDto(srpDto);
		List<SaleReturnPmo> srpList = srpResList.getEntity();
		Map<String, SaleReturnPmo> srpMap = new HashMap<String, SaleReturnPmo>();
		if(null != srpList && srpList.size() > 0){
			for(SaleReturnPmo srp: srpList){
				String key = srp.getSaleReturn()+"-"+srp.getObjectType()+"-"+srp.getObjectId()+"-"+year+"-"+srp.getSaleMonth();
				srpMap.put(key, srp);
			}
		}

		if(null != projectList && projectList.size() > 0){
			for(Project pro: projectList){
				for(int ii=0; ii<monthArr.length; ii++){
					srpDto = new SaleReturnPmoDto();
					srpDto.setObjectType(ObjectTypeEnum.PROJECT.getNo());
					srpDto.setObjectId(pro.getId());
					srpDto.setSaleYear(year);
					srpDto.setSaleMonth(monthArr[ii]);
					srpDto.setSalePlan("0");
					srpDto.setSaleActual("0");
					srpDto.setSaleDiff("0");
					srpDto.setCreateUserId(1L);
					srpDto.setDelFlag(DelFlagEnum.UNDEL.getNo());

					String saleKey = SaleReturnEnum.SALE.getNo()+"-"+ObjectTypeEnum.PROJECT.getNo()
							+"-"+pro.getId()+"-"+year+"-"+monthArr[ii];
					SaleReturnPmo salePmo = srpMap.get(saleKey);
					if(null == salePmo){
						srpDto.setSaleReturn(SaleReturnEnum.SALE.getNo());
						saleReturnPmoServiceFacade.save(srpDto);
					}

					String returnKey = SaleReturnEnum.RETURN.getNo()+"-"+ObjectTypeEnum.PROJECT.getNo()
							+"-"+pro.getId()+"-"+year+"-"+monthArr[ii];
					SaleReturnPmo returnPmo = srpMap.get(returnKey);
					if(null == returnPmo){
						srpDto.setSaleReturn(SaleReturnEnum.RETURN.getNo());
						saleReturnPmoServiceFacade.save(srpDto);
					}
				}
			}
		}

		ProjectPremisesDto ppDto = new ProjectPremisesDto();
		ppDto.setMerchantId(Long.valueOf(MerchantEnum.GREEN_TOWN.getNo()));
		ppDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		ResponseList<ProjectPremises> ppResList = projectPremisesServiceFacade.findByDto(ppDto);
		List<ProjectPremises> ppList = ppResList.getEntity();
		if(null != ppList && ppList.size() > 0){
			for(ProjectPremises pp: ppList){
				for(int ii=0; ii<monthArr.length; ii++){
					srpDto = new SaleReturnPmoDto();
					srpDto.setObjectType(ObjectTypeEnum.PREMISES.getNo());
					srpDto.setObjectId(pp.getId());
					srpDto.setSaleYear(year);
					srpDto.setSaleMonth(monthArr[ii]);
					srpDto.setSalePlan("0");
					srpDto.setSaleActual("0");
					srpDto.setSaleDiff("0");
					srpDto.setCreateUserId(1L);
					srpDto.setDelFlag(DelFlagEnum.UNDEL.getNo());

					String saleKey = SaleReturnEnum.SALE.getNo()+"-"+ObjectTypeEnum.PREMISES.getNo()
							+"-"+pp.getId()+"-"+year+"-"+monthArr[ii];
					SaleReturnPmo salePmo = srpMap.get(saleKey);
					if(null == salePmo){
						srpDto.setSaleReturn(SaleReturnEnum.SALE.getNo());
						saleReturnPmoServiceFacade.save(srpDto);
					}

					String returnKey = SaleReturnEnum.RETURN.getNo()+"-"+ObjectTypeEnum.PREMISES.getNo()
							+"-"+pp.getId()+"-"+year+"-"+monthArr[ii];
					SaleReturnPmo returnPmo = srpMap.get(returnKey);
					if(null == returnPmo){
						srpDto.setSaleReturn(SaleReturnEnum.RETURN.getNo());
						saleReturnPmoServiceFacade.save(srpDto);
					}

				}
			}
		}
	}

	/**
	 * 改变用户等级
	 */
	@RequestMapping(value = "changeCustomerLevel")
	public void changeCustomerLevel(Long projectId) {
		CustomerDto customerDto = new CustomerDto();
		if(null == projectId){
			return;
		}
		customerDto.setProjectId(projectId);
		customerDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		ResponseList<Customer> customerRes = customerServiceFacade.selectCustomerListByDto(customerDto);
		if(customerRes.isSuccess()){
			List<Customer> customerList = customerRes.getEntity();
			if(null != customerList && customerList.size() > 0){
				for(Customer customer: customerList){
					customerServiceFacade.changeCustomerLevel(customer.getId(), null);
				}
			}
		}
	}
}
